package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.WmsSupplierDTO;
import com.hxzy.entity.WmsSupplier;
import com.hxzy.mapper.WmsSupplierMapper;
import com.hxzy.service.WmsSupplierService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class WmsSupplierServiceImpl implements WmsSupplierService {
    @Override
    public BootstrapTableVO search(WmsSupplierDTO wmsSupplierDTO) {
        BootstrapTableVO bootstrapTableVO=new BootstrapTableVO();
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);

            //设置分页
            PageHelper.startPage(wmsSupplierDTO.getPageNumber(),wmsSupplierDTO.getPageSize());

            List<WmsSupplier> search = mapper.search(wmsSupplierDTO);

            PageInfo<WmsSupplier> pg=new PageInfo<>(search);

            bootstrapTableVO.setTotal(pg.getTotal());
            bootstrapTableVO.setRows(search);

        }
        return bootstrapTableVO;
    }

    @Override
    public boolean insert(WmsSupplier record) {
       int count=0;
       try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
           WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);
           count=mapper.insert(record);
           session.commit();
       }
       return count>0;
    }

    @Override
    public ResultVO<String> upDeleteByPrimaryKey(Integer id) {
        int count = 0;
        try(SqlSession session = MybatisUtil.getSqlSessionFactory().openSession()){
            WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);
            count = mapper.upDeleteByPrimaryKey(id);
            session.commit();
        }
        ResultVO<String> vo = null;
        if (count>0){
            vo = ResultVO.success("删除成功");
        }else {
            vo = ResultVO.failed(ResultEnum.DB_DELETE_FAILED);
        }
        return vo;
    }

    @Override
    public boolean updateByPrimaryKey(WmsSupplier record) {
        int count=0;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);
            count=mapper.updateByPrimaryKey(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public WmsSupplier selectByPrimaryKey(Integer id) {
        WmsSupplier wmsSupplier=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);
            wmsSupplier = mapper.selectByPrimaryKey(id);
        }
        return wmsSupplier;
    }

    @Override
    public List<WmsSupplier> selectAll() {
        List<WmsSupplier> list=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            WmsSupplierMapper mapper = session.getMapper(WmsSupplierMapper.class);
            list=mapper.selectAll();
        }
        return list;
    }
}
